package com.lrw.util;

import java.util.ArrayList;
import java.util.Collections;
import java.util.*;

import org.apache.commons.beanutils.BeanComparator;
import org.apache.commons.collections.ComparatorUtils;
import org.apache.commons.collections.comparators.ComparableComparator;
import org.apache.commons.collections.comparators.ComparatorChain;

public class ComparatorUtil {

	public static void Comparator(List list, String sortFieldName, boolean isReversed) {
		// 创建一个排序规则
		Comparator mycmp = ComparableComparator.getInstance();
		mycmp = ComparatorUtils.nullLowComparator(mycmp); // 允许null
		mycmp = ComparatorUtils.reversedComparator(mycmp); // 逆序

		// 声明要排序的对象的属性，并指明所使用的排序规则，如果不指明，则用默认排序
		ArrayList<Object> sortFields = new ArrayList<Object>();
		if (isReversed)
			sortFields.add(new BeanComparator(sortFieldName, mycmp)); // id逆序															// (主)
		else
			sortFields.add(new BeanComparator(sortFieldName)); // id逆序 (主)

		// 创建一个排序链
		ComparatorChain multiSort = new ComparatorChain(sortFields);

		// 开始真正的排序，按照先主，后副的规则
		Collections.sort(list, multiSort);
	}

}
